<?php
/**
 * Created by PhpStorm.
 * User: JX
 * Date: 2017/8/25
 * Time: 10:16
 */

namespace App\Http\Controllers\API\WeChat;


use App\Http\Controllers\Controller;
use App\Models\WechatArticle;
use App\Models\WechatArtSource;
use App\Models\WeChatImgLog;
use App\Models\WeChatImgSource;
use Illuminate\Http\Request;

class WeChatArtSourceController extends Controller
{
    public function index(Request $request)
    {
        $queryString = $request->get('query', '');
        $models = WechatArtSource::with('contexts')
              ->whereHas('contexts', function ($query)use ($queryString) {
                  $query->where('title', 'like', '%'.$queryString.'%');
              })->orderBy('id','desc')
            ->paginate(18);
        return $models;
    }


    /**
     * 添加科室
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
       $contents = $request->get('contexts');
        \DB::beginTransaction();
        try {
       $source = new WechatArtSource();
       if(!$source->save()){
           throw new \Exception(var_dump($source->getErrors()));
       }
       foreach ($contents as $key => $items) {
           $article = new WechatArticle();
           $article->title = $items['title'];
           $article->thumb_url = $items['thumb_url'];
           $article->show_cover_pic = $items['show_cover_pic'];
           $article->author = $items['author'];
           $article->digest = $items['digest'];
           $article->content = $items['content'];
           $article->content_source_url = $items['content_source_url'];
           $article->sources_id = $source->id;
           $article->index = $key;
           if(!$article->save()){
               throw new \Exception(var_dump($article->getErrors()));
           }
       }
            \DB::commit();
            return response('添加成功', 200);
       }catch (\Exception $e){
            \DB::rollBack();
            return response($e->getMessage(), 406);
        }
    }


    /**
     * 修改员工
     * @param User $User
     * @param Request $request
     * @return mixed
     */
    public function update( Request $request)
    {
        $contents = $request->get('contexts');
        $id = $request->get('id');
        \DB::beginTransaction();
        try {
            $source = $this->findModel($id);
            WechatArticle::query()->where(['sources_id'=>$id])->delete();
            foreach ($contents as $key => $items) {
                $article = new WechatArticle();
                $article->title = $items['title'];
                $article->thumb_url = $items['thumb_url'];
                $article->show_cover_pic = $items['show_cover_pic'];
                $article->author = $items['author'];
                $article->digest = $items['digest'];
                $article->content = $items['content'];
                $article->content_source_url = $items['content_source_url'];
                $article->sources_id = $source->id;
                $article->index = $key;
                if(!$article->save()){
                    throw new \Exception(var_dump($article->getErrors()));
                }
            }
            \DB::commit();
            return response('修改成功', 200);
        }catch (\Exception $e){
            \DB::rollBack();
            return response($e->getMessage(), 406);
        }
    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $model = $this->findModel($id);
        try {
            WechatArticle::query()->where(['sources_id'=>$id])->delete();
            $model->delete();
            return ['state' => 'success'];
        } catch (\Exception $e) {
            return ['state' => 'failed', 'message' => $e->getMessage()];
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show($id)
    {
        return $this->findModel($id);
    }




    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {
        $model = WechatArtSource::with('contexts')->where('id',$id)->first();
        return $model;
    }
}